const Room = require('./room');
const Dispatcher = require('./dispatcher');
// const axios = require('axios');
const md5 = require('md5');
const path = require('path');
const fs = require('fs');
const extend = require('extend');

const dirPlayer = '__players';

class Player extends Dispatcher {
    constructor(server, id, name, password) {
        super();
        this.server = server;
        this.id = id;

        if (server.config.login) {
            name = id;
            let dicPath = path.join(path.resolve(server.config.assets), dirPlayer);
            if (!fs.existsSync(dicPath)) fs.mkdirSync(dicPath);
            if (fs.statSync(dicPath).isDirectory()) {
                let playerPath = path.join(dicPath, id);
                if (fs.existsSync(playerPath)) {
                    let player = JSON.parse(fs.readFileSync(playerPath));
                    this.fromFile(player);
                    this.id = player.id;
                    this.name = player.name;
                    this.password = player.password;
                } else {
                    this.init(id, name);
                    this.password = md5(password);
                    this.saveToFile();
                }
                this.on(() => this.saveToFile());
            } else {
                console.error('__players 文件夹被占用');
                throw '配置错误';
            }
        } else {
            this.name = name;
        }
        this.leaved = false;

        console.log(`新用户${ this.name || this.id }登录了`);
    }
    saveToFile() {
        fs.writeFileSync(path.join(path.resolve(this.server.config.assets), dirPlayer, this.id), JSON.stringify(extend(true, this.toFile(), {
            id: this.id,
            name: this.name,
            password: this.password
        })));
    }
    init(id, name) {
        this.id = id;
        this.name = name;
    }
    fromFile(player) {
        this.id = player.id;
        this.name = player.name;
        this.password = player.password;
    }
    toFile() {
        return {
            id: this.id,
            name: this.name,
            password: this.password
        }
    }
    createRoom(settings) {
        if (this.room) {
            if (this.room.creator === this) {
                this.room.settings = settings;
            } else {
                throw '';
            }
        } else {
            this.room = new (this.server.config.Room || Room)(this.server, this, settings);
        }
    }
    joinRoom(roomId) {
        if (this.room) {
            throw '';
        } else {
            Room.get(this.server, roomId).join(this);
        }
    }
    startGame() {
        if (this.room) {
            if (this.room.creator === this) {
                this.room.start();
            } else {
                throw '';
            }
        } else {
            throw '';
        }
    }
    leave() {
        this.leaved = true;
        if (this.room) this.room.leave();
        console.log(`用户${ this.name || this.id }掉线了`);
    }
    get role() {
        if (this.room) {
            return this.room.roles.find(role => role.player === this);
        } else {
            throw '';
        }
    }
    get game() {
        if (this.room) {
            return this.room.game;
        } else {
            throw '';
        }
    }
    get simple() {
        return {
            id: this.id,
            name: this.name,
            leaved: this.leaved
        }
    }
    get data() {
        return {
            id: this.id,
            name: this.name,
            leaved: this.leaved,
            room: this.room && this.room.data,
            role: this.room && this.role.data
        }
    }
	static get(server, id, name, password) {
        if (!server.players) server.players = new Map();
        if (server.players.has(id)) {
            let player = server.players.get(id);
            if (name && typeof name !== 'object') {
                if ((player.name || player.id) !== name) {
                    console.log(`用户${ player.name || player.id }修改昵称为${ name }`);
                }
                player.name = name;
                if (player.room) {
                    player.room.dispatch();
                }
            }
            return player;
        } else {
            let player = new (server.config.Player || Player)(server, id, name, password);
            server.players.set(id, player);
            return player;
        }
    }
    static login(server, id, name, password) {
        let player = Player.get(server, id, name, password);
        if (server.config.login) {
            if (player.password !== md5(password)) throw '账号或密码错误';
        }
        if (player.leaved) console.log(`用户${ player.name || player.id }重新上线了`);
        player.leaved = false;
        return player;
    }
}

module.exports = Player;